package com.pkk.penguin.util.image.support.utils;

import com.pkk.penguin.util.image.support.enums.ImageTypeEnum;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.apache.commons.lang3.StringUtils;

/**
 * @author peikunkun
 * @version V1.0
 * @Title: qrcode
 * @Package com.ningpai.qrcode.utils
 * @Description: <二维码拼图操作流转换工具类>
 * @date 2018/7/7 0007 下午 7:32
 */
public class ImageStreamUtil {


  /**
   * 将bufferedImage流转换为inputStream流
   *
   * @param bufferedImage
   * @return
   */
  public static InputStream bufferedImageToInputStream(BufferedImage bufferedImage) {
    if (bufferedImage == null) {
      return null;
    }
    //转换BufferedImage为inputStream
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    InputStream inputStream = null;
    try {
      ImageIO.write(bufferedImage, ImageTypeEnum.PNG.getImageType(), byteArrayOutputStream);
      inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
      byteArrayOutputStream.flush();
      byteArrayOutputStream.close();
      bufferedImage.flush();
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
    return inputStream;
  }


  /**
   * 将outputStream转换为inputStream
   *
   * @param outputStream
   * @return
   */
  public static InputStream outputStreamToInputStream(OutputStream outputStream) {
    if (outputStream == null) {
      return null;
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byteArrayOutputStream = (ByteArrayOutputStream) outputStream;
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    return byteArrayInputStream;
  }


  /**
   * 将inputStream转换为outputStream
   *
   * @param inputStream
   * @return
   */
  public static OutputStream inputStreamToOutputStream(InputStream inputStream) throws IOException {
    if (inputStream == null) {
      return null;
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;
    while ((i = inputStream.read()) != -1) {
      byteArrayOutputStream.write(i);
    }
    return byteArrayOutputStream;
  }

  /**
   * 获取网络图片流
   *
   * @param url
   * @return
   */
  public static InputStream getImageStream(String url) {
    if (StringUtils.isBlank(url)) {
      return null;
    }
    try {
      HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
      connection.setReadTimeout(5000);
      connection.setConnectTimeout(5000);
      connection.setRequestMethod("POST");
      connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
      if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        InputStream inputStream = connection.getInputStream();
        return inputStream;
      }
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
    return null;
  }


  /**
   * Conn 读取网络图片
   *
   * @param imageUrl
   * @return
   */
  public static InputStream imageUrlToBufferedImage(String imageUrl) {
    if (StringUtils.isBlank(imageUrl)) {
      return null;
    }
    URL url = null;
    InputStream is = null;
    try {
      url = new URL(imageUrl);
      is = url.openConnection().getInputStream();
    } catch (MalformedURLException e) {
      e.printStackTrace();
      return null;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
    return is;


  }


}
